package com.maple.springbootinit.api;

import okhttp3.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class OpenAIAPI {

    private final OkHttpClient client = new OkHttpClient();
    private static final String API_KEY = "sk-3t3VZI9gEbA4XAtvSuIWaccQMIePSxHycsoSFSRMi3FefVln";
    private static final String URL = "https://api.gptgod.online/v1/chat/completions";

    public String  getChatGptResponse(String model, String prompt) throws IOException {
        RequestBody body = RequestBody.create(MediaType.get("application/json; charset=utf-8"),
                "{\"model\": \"" + model + "\", \"messages\": [{\"role\": \"system\", \"content\": " +
                        "\"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": " + prompt + "}]}");

        Request request = new Request.Builder()
                .url(URL)
                .post(body)
                .addHeader("Authorization", "Bearer " + API_KEY)
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
//            System.out.println(response.body().string());
        }
    }

//    public static void main(String[] args) throws IOException {
//        OpenAIAPI api = new OpenAIAPI();
//        api.getChatGptResponse("gpt-3.5-turbo", "Hello, how are you?");
//    }
}
